package com.seqb.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.seqb.common.ResultVo;
import com.seqb.exception.BdException;
import com.seqb.manage.subject.dao.SubjectDao;
import com.seqb.manage.subject.domain.SubjectDO;
import com.seqb.manage.user.dao.UserSubjectDao;
import com.seqb.manage.user.domain.UserSubjectDO;
import com.seqb.server.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2019/12/24
 */
@Service
public class ServerServiceImpl implements ServerService {

    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private UserSubjectDao userSubjectDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo subjectSelect(UserSubjectDO userSubject) {
        try {
            //参数校验
            Long userId = userSubject.getUserId();
            Long subjectId = userSubject.getSubjectId();
            if (null == userId || null == subjectId) {
                return ResultVo.error("参数不足！");
            }
            SubjectDO subject = subjectDao.selectById(subjectId);
            if (null == subject) {
                return ResultVo.error("科目不存在！");
            }
            String subjectName = subject.getName();
            //将所选科目设置为0
            UserSubjectDO updateDO = new UserSubjectDO();
            updateDO.setCurrentChoice("0");
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("user_id", userId);
            userSubjectDao.update(updateDO, updateWrapper);
            //设置所选的科目
            int result;
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("subject_id", subjectId);
            UserSubjectDO existDO = userSubjectDao.selectOne(queryWrapper);
            if (null == existDO) {
                //新增
                UserSubjectDO saveDO = new UserSubjectDO();
                saveDO.setUserId(userId);
                saveDO.setSubjectId(subjectId);
                saveDO.setSubjectName(subjectName);
                saveDO.setCurrentChoice("1");
                result = userSubjectDao.insert(saveDO);
            } else {
                //更新
                updateDO = new UserSubjectDO();
                updateDO.setId(existDO.getId());
                updateDO.setSubjectId(subjectId);
                updateDO.setSubjectName(subjectName);
                updateDO.setCurrentChoice("1");
                result = userSubjectDao.updateById(updateDO);
            }
            if (result <= 0) {
                throw new BdException("操作失败！");
            }

            //返回选择科目的信息
            Map<String, Object> data = new HashMap<>();
            data.put("subjectId", subject.getId());
            data.put("subjectName", subject.getName());
            data.put("firstTypeId", subject.getFirstTypeId());
            data.put("secondTypeId", subject.getSecondTypeId());
            return ResultVo.ok().put("data", data);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResultVo.error();
    }

    @Override
    public ResultVo userSubject(UserSubjectDO userSubject) {
        try {
            Long userId = userSubject.getUserId();
            if (null == userId) {
                return ResultVo.error("参数不足！");
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("current_choice", "1");
            UserSubjectDO existDO = userSubjectDao.selectOne(queryWrapper);
            Map<String, Object> result = new HashMap<>();
            if (null != existDO) {
                Long subjectId = existDO.getSubjectId();
                SubjectDO subject = subjectDao.selectById(subjectId);
                result.put("subjectId", subjectId);
                result.put("subjectName", subject.getName());
                result.put("firstTypeId", subject.getFirstTypeId());
                result.put("secondTypeId", subject.getSecondTypeId());
            }
            return ResultVo.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error();
    }

}
